package com.lanou.renrenxin.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.singleton.RenrenxinSingleton;
import com.lanou.renrenxin.tools.RenRenXinHelper;
import com.parse.ParseAnalytics;


/**
 * Created by dllo on 16/3/2.
 */
public class RegisterActivity extends BaseActivity {
    private EditText usernameEt,passWordEt,passWordAgainEt;
    private TextView textViewOne,textViewTwo;
    private Context context;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        setContentView(R.layout.activity_register);

        //appLogo文字效果设置
        textViewOne = (TextView) findViewById(R.id.register_renRenXin);
        textViewTwo = (TextView) findViewById(R.id.register_jiShiTongXun);
        textViewOne.setRotationY(20);
        textViewTwo.setRotationY(-20);

        context = getApplicationContext();
        usernameEt = (EditText) findViewById(R.id.userName_register);
        passWordEt = (EditText) findViewById(R.id.passWord_register);
        passWordAgainEt = (EditText) findViewById(R.id.passWordAgain_register);
    }

    /**
     * 注册
     *
     * @param view
     */
    public void register(View view) {
         final String username = usernameEt.getText().toString().trim();
         final String passWord = passWordEt.getText().toString().trim();
        String confirm_pwd = passWordAgainEt.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
            usernameEt.requestFocus();
            return;
        } else if (TextUtils.isEmpty(passWord)) {
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            passWordEt.requestFocus();
        } else if (TextUtils.isEmpty(confirm_pwd)) {
            Toast.makeText(this, "请再次确认密码！", Toast.LENGTH_SHORT).show();
            passWordAgainEt.requestFocus();
        } else if (!passWord.equals(confirm_pwd)) {
            Toast.makeText(this, "两次输入的密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(passWord)) {
            final ProgressDialog pd = new ProgressDialog(this);
            pd.setMessage(getResources().getString(R.string.Is_the_registered));
            pd.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 调用sdk注册方法
                        EMChatManager.getInstance().createAccountOnServer(username, passWord);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (!RegisterActivity.this.isFinishing())
                                    pd.dismiss();
                                //保存用户名
                                RenrenxinSingleton.getInstence().saveUserData(username,"RiSky57",passWord,null,
                                        "大连","十万代码成就十万年薪","男","东北");
                                RenRenXinHelper.getInstance().setCurrentUserName(username);
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    } catch (final EaseMobException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (!RegisterActivity.this.isFinishing())
                                    pd.dismiss();
                                int errorCode=e.getErrorCode();
                                if(errorCode== EMError.NONETWORK_ERROR){
                                    Toast.makeText(getApplicationContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
                                }else if(errorCode == EMError.USER_ALREADY_EXISTS){
                                    Toast.makeText(getApplicationContext(), "该用户名已被注册！", Toast.LENGTH_SHORT).show();
                                }else if(errorCode == EMError.UNAUTHORIZED){
                                    Toast.makeText(getApplicationContext(), "注册失败，无权限！", Toast.LENGTH_SHORT).show();
                                }else if(errorCode == EMError.ILLEGAL_USER_NAME){
                                    Toast.makeText(getApplicationContext(), "非法的用户名！",Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(getApplicationContext(), "注册失败！" + e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            }).start();

        }

    }


    public void back(View view){
        finish();
    }
}
